package com.lx.chong.aspect;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * @author chong
 * @create 2019/3/12
 * Desc:
 */
@Configuration
@Component
public class InterceptorConfig extends WebMvcConfigurationSupport {
    private static final String FAVICON_URL = "/favicon.ico";

    /**
     *     * 发现如果继承了WebMvcConfigurationSupport，则在yml中配置的相关内容会失效。
     *     *
     *     * @param registry
     *    
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/").addResourceLocations("/**");
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }

    /**
     *     * 配置servlet处理
     *    
     */

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //设置（模糊）匹配的url
        registry.addInterceptor(authTokenInterceptor()).addPathPatterns("/*").excludePathPatterns(FAVICON_URL);
        super.addInterceptors(registry);
    }

    //将拦截器作为bean写入配置中
    @Bean
    public AuthTokenInterceptor authTokenInterceptor() {
        return new AuthTokenInterceptor();
    }
}
